/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */

package com.hlingsoft.kkh.action;


import com.hlingsoft.kkh.dao.*;
import com.hlingsoft.kkh.entity.AppUser;

import com.hlingsoft.kkh.entity.EventComments;
import com.hlingsoft.kkh.entity.Events;
import com.hlingsoft.kkh.entity.Focus;

import com.hlingsoft.kkh.entity.vo.StartsVo;
import com.hlingsoft.kkh.service.AdminPushService;
import com.hlingsoft.kkh.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 用户
 *
 * @author Herbert
 */
@Controller
@RequestMapping("/focus")
public class FocusAction extends BaseAction {


    @Autowired
    protected MerchantDao merchantDao;

    private static final int rows = 10;

    @Autowired
    private FocusDao focusDao;

    @Autowired
    private EventCommentsDao eventCommentsDao;

    @Autowired
    private EventsDao eventsDao;


    final Character[] characters = new Character[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'I', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',};


    /**
     * 添加关注
     *
     * @param userId
     * @param focusId
     * @param request
     * @return
     */
    @RequestMapping(value = "/add.json")
    @ResponseBody
    public Map<String, Object> add(
            @RequestParam(value = "userId") long userId,
            @RequestParam(value = "focusId") long focusId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            if (userId!=focusId){
                Focus checkFocus = focusDao.getFocus(userId, focusId);
                if (checkFocus == null) {
                    Focus focus = new Focus();
                    focus.setFansId(userId);
                    focus.setAttentionsId(focusId);
                    focusDao.addFocus(focus);

                    //极光推送
                    final AppUser user = userDao.getUserById(userId);
                    final AppUser focusUser = userDao.getUserById(focusId);
                    String alert = user.getName() + "关注了您";
                    adminPushService.pushToClient(alert, new ArrayList<String>() {
                        {
                            add(focusUser.getUdId());
                        }
                    }, AdminPushService.JOB, false,true);
                }
                resultMap.put("result", true);
            }
            else {
                resultMap.put("msg","自己不能关注自己");
                resultMap.put("result",false);
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 取消关注
     *
     * @param userId
     * @param focusId
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete.json")
    @ResponseBody
    public Map<String, Object> delete(
            @RequestParam(value = "userId") long userId,
            @RequestParam(value = "focusId") long focusId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Focus focus = focusDao.getFocus(userId, focusId);
            if (focus != null) {
                focus.setAvailable(0);
                focusDao.updateFocus(focus);
            }
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 关注人列表
     *
     * @param userId
     * @param request
     * @return
     */
    @RequestMapping("/attentionsList.json")
    @ResponseBody
    public Map<String, Object> focus(
            @RequestParam(value = "userId") long userId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        TreeMap<Character, List<StartsVo>> startsVoMap = new TreeMap<Character, List<StartsVo>>();
        try {
            List<Focus> focusesList = focusDao.getAttentionsById(userId,0,100);
            for (Focus focus : focusesList) {

                //粉丝用户
                StartsVo startsVo = new StartsVo();

                //获取关注人
                AppUser appUser = userDao.getUserById(focus.getAttentionsId());
                //获取关注人粉丝数
                long fansCount = focusDao.getFansByIdCount(appUser.getId());
                appUser.setFansCount(fansCount);

                //粉丝最新发布的评论
                Events events= eventsDao.getNewestEventsByUserId(appUser.getId());

                //判断是否可以关注
                if (focusDao.getFocus(appUser.getId(),userId) == null) {
                    appUser.setIsFocus(1);
                } else {
                    appUser.setIsFocus(2);
                }

                startsVo.setAppUser(appUser);
                startsVo.setNewestEvents(events);


                //首字母排序
                String name = appUser.getName();
                Character c = Common.getPinYin(name);
                c = Character.toUpperCase(c);


                boolean flag = true;
                for (Character character : characters) {
                    if (c.equals(character))
                        flag = false;
                }

                if (flag) {
                    if (startsVoMap.containsKey('!')) {
                        List<StartsVo> startsVoList = startsVoMap.get('!');
                        startsVoList.add(startsVo);
                        startsVoMap.put('!', startsVoList);
                    } else {
                        List<StartsVo> startsVoList = new ArrayList<StartsVo>();
                        startsVoList.add(startsVo);
                        startsVoMap.put('!', startsVoList);
                    }

                } else {
                    if (startsVoMap.containsKey(c)) {
                        List<StartsVo> startsVoList = startsVoMap.get(c);
                        startsVoList.add(startsVo);
                        startsVoMap.put(c, startsVoList);
                    } else {
                        List<StartsVo> startsVoList = new ArrayList<StartsVo>();
                        startsVoList.add(startsVo);
                        startsVoMap.put(c, startsVoList);
                    }
                }

                List<Map.Entry<Character, List<StartsVo>>> entrys = new ArrayList<>(startsVoMap.entrySet());

                Collections.sort(entrys, new Comparator<Map.Entry<Character, List<StartsVo>>>() {
                    @Override
                    public int compare(Map.Entry<Character, List<StartsVo>> o1, Map.Entry<Character, List<StartsVo>> o2) {
                        return o2.getKey().compareTo(o1.getKey());
                    }
                });

            }

            resultMap.put("startsVoMap", startsVoMap);
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 粉丝列表
     *
     * @param userId
     * @param request
     * @return
     */

    @RequestMapping("/fansList.json")
    @ResponseBody
    public Map<String, Object> fans(
            @RequestParam(value = "userId") long userId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        TreeMap<Character, List<StartsVo>> startsVoMap = new TreeMap<Character, List<StartsVo>>();

        try {
            //获取粉丝
            List<Focus> focusesList = focusDao.getFansById(userId,0,100);


            for (Focus focus : focusesList) {
                //粉丝用户
                StartsVo startsVo = new StartsVo();

                //获取粉丝
                AppUser appUser = userDao.getUserById(focus.getFansId());

                //获取粉丝的粉丝数
                if (appUser!=null) {
                    long fansCount = focusDao.getFansByIdCount(appUser.getId());
                    appUser.setFansCount(fansCount);
                    //粉丝最新发布的评论
                    Events events = eventsDao.getNewestEventsByUserId(appUser.getId());
                    startsVo.setAppUser(appUser);
                    startsVo.setNewestEvents(events);


                    //搜字母排序
                    String name = appUser.getName();
                    if (focusDao.getFocus(userId, appUser.getId()) == null) {
                        appUser.setIsFocus(0);
                    } else {
                        appUser.setIsFocus(2);
                    }

                    Character c = Common.getPinYin(name);
                    c = Character.toUpperCase(c);


                    boolean flag = true;
                    for (Character character : characters) {
                        if (c.equals(character))
                            flag = false;
                    }

                    if (flag) {
                        if (startsVoMap.containsKey('!')) {
                            List<StartsVo> startsVoList = startsVoMap.get('!');
                            startsVoList.add(startsVo);
                            startsVoMap.put('!', startsVoList);
                        } else {
                            List<StartsVo> startsVoList = new ArrayList<StartsVo>();
                            startsVoList.add(startsVo);
                            startsVoMap.put('!', startsVoList);
                        }

                    } else {
                        if (startsVoMap.containsKey(c)) {
                            List<StartsVo> startsVoList = startsVoMap.get(c);
                            startsVoList.add(startsVo);
                            startsVoMap.put(c, startsVoList);
                        } else {
                            List<StartsVo> startsVoList = new ArrayList<StartsVo>();
                            startsVoList.add(startsVo);
                            startsVoMap.put(c, startsVoList);
                        }
                    }
                    List<Map.Entry<Character, List<StartsVo>>> entrys = new ArrayList<>(startsVoMap.entrySet());

                    Collections.sort(entrys, new Comparator<Map.Entry<Character, List<StartsVo>>>() {
                        @Override
                        public int compare(Map.Entry<Character, List<StartsVo>> o1, Map.Entry<Character, List<StartsVo>> o2) {
                            return o2.getKey().compareTo(o1.getKey());
                        }
                    });


                }


            }

            resultMap.put("startsVoMap", startsVoMap);
            resultMap.put("result", true);

        } catch (
                Exception e
                )

        {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;
    }
}
